package cn.wind.xboot.core.utils;

import cn.hutool.core.codec.BCD;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

/**
 * @author xukk
 * @date 2018/5/4.
 */

public class RSAUtil {
    private RSA rsa;
    private String privateKey;
    private String publicKey;
    private KeyType keyType;

    public String encrypt(String data) {
        String encode = rsa.encryptStr(data, keyType);
        return java.util.Base64.getEncoder().encodeToString(BCD.strToBcd(encode));
    }

    public String decrypt(String encodeData) {
        String encode = BCD.bcdToStr(java.util.Base64.getDecoder().decode(encodeData));
        return rsa.decryptStr(encode, keyType);
    }
    public String encryptSafe(String data) {
        String encode = rsa.encryptStr(data, keyType);
        return java.util.Base64.getUrlEncoder().encodeToString(BCD.strToBcd(encode));
    }

    public String decryptSafe(String encodeData) {
        String encode = BCD.bcdToStr(java.util.Base64.getUrlDecoder().decode(encodeData));
        return rsa.decryptStr(encode, keyType);
    }
    public RSAUtil privateKey(String privateKey) {
        this.privateKey = privateKey;
        return this;
    }

    public RSAUtil publicKey(String publicKey) {
        this.publicKey = publicKey;
        return this;
    }

    public RSAUtil keyType(KeyType keyType) {
        this.keyType = keyType;
        return this;
    }

    public RSAUtil rsa() {
        this.rsa = SecureUtil.rsa(privateKey, publicKey);
        return this;
    }

    public static RSAUtil build() {
        return new RSAUtil();
    }

    public RSAUtil initKeys() {
        this.rsa = SecureUtil.rsa();
        rsa.initKeys();
        return this;
    }

    public String getPrivateKeyBase64() {
        return this.rsa.getPrivateKeyBase64();
    }

    public String getPublicKeyBase64() {
        return this.rsa.getPublicKeyBase64();
    }

    public static void main(String[] args) {
        //RSAUtil rsaUtil=RSAUtil.build().initKeys();
        System.err.println("公钥加密——私钥解密");
        RSAUtil tt = RSAUtil.build().initKeys();
        String data = "{'tax_territoriality': '浙江杭州', 'tax_company': '浙江瑞登资产管理有限公司'}";
        String privateKey ="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAL6K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+Fvi/dJu+2DmAZSVQDQxrzKnWULX3l611rNz/Twl0gNLBcElyBKRYPlj/3xWJNB93aA1kc0BdYMK3ZITcLouOPDf4dH2Ei8IGGX5jcZqJdKXPkHoYZOMfLsQJBAOo4pSaI2sWFtWN4r8GGdSayzQ2iPsSrO6ld8uLc/S90+n3X2PNmS+hvUE7mLFtHrrvUlO8IJ8doQc2DjmziSGkCQQDnLx5CBr0AosVF8+dnBn64mK54MM6Ojd2uhl0K/1y+yQ/loeJDLUtcj+tLevOV4xCcdC5UwNbSyYsGAJGANHx/AkB6vNpWeirijTHDgY/ggvxLHjF0Loxj9TNbip7oIVQQFWXaokzPJ9EpaYOVBOtDninDxh+Rrx1hUpIBByqQhnfZAkBRdTTAJweplbDU+R/sGUnFWWcASpI9gJc/y3A1U80mHEpRqzzCVs9bCgrqSaRSh/NubTF2A4yuMP2fW8vo1JWZAkEA2Er9IiIDcB7K47TKELBR4ja2+4D5KKYZAQo2hK2gwcTPnfU0ksqvuk5wrAzcaMVxnmyHoxNvM06pvQKiuAHd2g==";
        //tt.getPrivateKeyBase64();
        System.out.println("privateKey:" + privateKey);
        //tt.getPrivateKeyBase64();
        //"MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALN1fn6m2+nh6RDyPc4LNeRkUICp2aeGuA+XvKRXFhU8vlJSCud3jNnDV93OZ8O6WmSSsepCJBTzAyrtOb+5fF4wSekF/zV8z0+OMxvEuHovwMgTvpYsLt3t3u8h+aLrXyo63iHevbxstiBRymUuwZFjR6PKHWSeaCqL8s9a/0JtAgMBAAECgYBKZuUJ114HinzZZMVQ/n7acOibX0/4z7bR6CPyspRaO7PgZ1lT+9Gumvl74U5N7J37YoJW0BMriFEA5Bd0MI+pvhlt9qbKYbykV6giZP4MIttn66u2UWoUBGtGLeOIlJFcXwF3Uckg6NlY/hWjpfZi6llfwpaur3ORWa/Rt9mpNQJBAPLFkUB3BaoxGQ0DHN2s37359MlNm6v0KuiXRGklEi19SRkS1U7vtQLbRF+ytITub1RFtEg7XhsD4Qy2/2Ir0dcCQQC9PMYxyA6T3livJIAovUwkA2tKyLIX1PfLjydooAs3Anq94wUmxpHbl7O14ZezB46g7gkikVWgfksMmM1FYk1bAkEAm/x/iLkxBZqIawjt+i0CkfFR7CoWdRTsoQfYp+pu1JWkaxzju9VfK9exBgAv5x3AoJgs7yBeJeOHLYFDdFONfwJAdtifRI53EUjX+4756hwQoKGBFaN7rdnzkeUjA+NXh8HH4k+cYaDd48Kfe7/lbOt3GPzFwe7yjuwiKBQi/stg9wJBAJwuXbk7PmUvYohKuXyT5u+VFZNvGfUQ00/uI2OU8SP55XdGA/VL6WRWiIHAIxKllHGL9txwYacU6mAZl507DAA=";
        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDThDkbI6/gpJbD6u5i7WGfg0G8zZJj02l+OSQdtMSdA6KEKWdI3yYmFBTGI4183xkfHa8QvffGM6myCcQt2Qh5c290EjVYpaWkMW3oSbh4j0QxqeLtatlLlZILfZDi/WVttrUwywr3kawyi0zrlqO6C8XFrgcadtmBrYYJ1KjIFwIDAQAB";
        //tt.getPublicKeyBase64();
        System.out.println("publicKey:" + publicKey);
        //"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCzdX5+ptvp4ekQ8j3OCzXkZFCAqdmnhrgPl7ykVxYVPL5SUgrnd4zZw1fdzmfDulpkkrHqQiQU8wMq7Tm/uXxeMEnpBf81fM9PjjMbxLh6L8DIE76WLC7d7d7vIfmi618qOt4h3r28bLYgUcplLsGRY0ejyh1knmgqi/LPWv9CbQIDAQAB";
        System.out.println("\r加密前文字：\r\n" + data);
        RSAUtil rsaUtil = RSAUtil.build().privateKey(privateKey).publicKey(publicKey).rsa();
        //String encodeStr = rsaUtil.keyType(KeyType.PublicKey).encrypt(data);
        //String encodeStr = java.util.Base64.getUrlEncoder().encodeToString(rsaUtil.keyType(KeyType.PublicKey).encrypt(data).getBytes());
        String encodeStr= "YzdGODY2eXJHQmg3U3pHbjBobjY2YStDb0lUdmJqejdVenBrU1JneFJRUStUenZLWDdyM2s1ME5PTG9NZWFLYXZnOW5pVE5ieXFIaEJFeHh0T1pPbHZrcVVKeHpOcnBXTlRvM0doKzBMQmdDMnhPb2xkRlo4QzZHWEE0NVpheVNLTnh2SHNtQzlET0I0YlRWUHg0UUZaWXVEbDdEWksybXd1Nzh2MnFFM0ZnPQ";
        System.out.println("\r加密后文字: \r\n" + encodeStr);
        String decodeStr = rsaUtil.keyType(KeyType.PrivateKey).decrypt(new String(java.util.Base64.getUrlDecoder().decode(encodeStr)));
        System.out.println("\r解密后文字: \r\n" + decodeStr);
    }
}
